package com.shrw.duke.bluetoothheartrate.adapter;

import android.app.Activity;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shrw.duke.bluetoothheartrate.OnMoveAndSwipedListener;
import com.shrw.duke.bluetoothheartrate.R;
import com.shrw.duke.bluetoothheartrate.entity.RateEntity;
import com.vise.baseble.model.BluetoothLeDevice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Created by rw-duke on 2017/11/3.
 */

public class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.ViewHolder> implements OnMoveAndSwipedListener, View.OnClickListener {
    private Activity mContext;
    private List<RateEntity> mDeviceList;

    private TextView mRate,mAddress;
    private ImageView imageView;
    private PopupWindow popupWindow;
    private int mCurrentPosition = -1;

    public BaseAdapter(Activity mContext) {
        this.mContext = mContext;
        mDeviceList = new ArrayList<>();

        initPopup();
    }

    public int addItem(RateEntity device) {
        int index = contains(device);
        if (index == -1){
            mDeviceList.add(device);
            notifyItemChanged(mDeviceList.size()-1);
        }else {
            mDeviceList.set(index,device);
            notifyItemChanged(index);
        }

        return index;

    }

    public int contains(RateEntity device) {
        Iterator<RateEntity> d = mDeviceList.iterator();
        int index = 0;
        while (d.hasNext()) {
            RateEntity device1 = d.next();
            if (device.getAddress().equals(device1.getAddress())) {
                return index;
            }
            index++;
        }
        return -1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final RateEntity device = mDeviceList.get(position);

//        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_recycler_item_show);
//        holder.itemView.startAnimation(animation);
//
//        AlphaAnimation aa1 = new AlphaAnimation(1.0f, 0.1f);
//        aa1.setDuration(400);
//        holder.rela_round.startAnimation(aa1);
//
//        AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
//        aa.setDuration(400);
//        holder.rela_round.startAnimation(aa);

        holder.name.setText(device.getName());
        holder.address.setText(device.getAddress());
        holder.record.setText(device.getRecord());
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);

        if (mCurrentPosition == position){
            setPopupValues(device);

        }
    }


    @Override
    public int getItemCount() {
        return mDeviceList.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return false;
    }

    @Override
    public void onItemDismiss(int position) {

    }

    @Override
    public void onClick(View v) {
        int index = (int) v.getTag();
        showPopup(index,v);
        Toast.makeText(mContext, mDeviceList.get(index).getRate(), Toast.LENGTH_SHORT).show();
    }

    public void clearAll() {
        mDeviceList.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, address,record;
        private RelativeLayout rela_round;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView)itemView.findViewById(R.id.tv_recycler_item_name);
            address = (TextView)itemView.findViewById(R.id.tv_recycler_item_address);
            record = (TextView)itemView.findViewById(R.id.tv_recycler_item_record);
            rela_round = (RelativeLayout)itemView.findViewById(R.id.rela_round);
        }
    }

    private void initPopup(){
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentview = inflater.inflate(R.layout.rate_popup_layout, null);

        popupWindow = new PopupWindow(contentview,WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,false);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
//        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(true);
        mRate = contentview.findViewById(R.id.rate_popup_rate);
        mAddress = contentview.findViewById(R.id.rate_tv_address);
        imageView = contentview.findViewById(R.id.rate_popup_img);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mCurrentPosition = -1;
            }
        });

        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.9f; //0.0-1.0
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        mContext.getWindow().setAttributes(lp);
    }


    public void showPopup(int index, View view){
        if (popupWindow != null){
            mCurrentPosition = index;
            mAddress.setText(mDeviceList.get(index).getName());
            setPopupValues(mDeviceList.get(index));
            popupWindow.showAtLocation(view,  Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
        }
    }

    public boolean isShowPopup(){
        return mCurrentPosition==-1 ? false : true;
    }


    private void setPopupValues(RateEntity device) {
        if (mRate!= null && imageView!= null){
            int rate = Integer.parseInt(device.getRate(),16);
            mRate.setText(rate+"\t次/分钟");
            if (rate<60){
                mRate.setTextColor(Color.parseColor("#d81e06"));
                imageView.setImageResource(R.drawable.rate_risk);
            } else if (rate<100) {
                mRate.setTextColor(Color.parseColor("#2C2C2C"));
                imageView.setImageResource(R.drawable.rate_normal);
            }else {
                mRate.setTextColor(Color.parseColor("#d81e06"));
                imageView.setImageResource(R.drawable.rate_risk);
            }
        }
    }

}
